import { login, logout, getInfo } from "@/api/login";
import { getCount } from "@/api/user";
import {
  getToken,
  setToken,
  removeToken,
  getUserInfo,
  setUserInfo,
  removeUserInfo,
} from "@/utils/auth";
import configs from "@/utils/config";
import { menuListFilter } from "@/utils/son";

const user = {
  state: {
    token: getToken(),
    userInfo: {},
    avatar: "",
    roles: [],
    rolesList: [],
    // 全屏
    fullscreen: false,
    resourceTree: {},
    permissions: [],
    orgList: [],
    messages: [],
  },

  mutations: {
    // 全屏
    SET_FULLSCREEN: (state, fullscreen) => {
      state.fullscreen = fullscreen;
    },
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_USER_INFO: (state, userInfo) => {
      state.userInfo = userInfo;
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    },
    SET_ROLES_LIST: (state, roles) => {
      state.rolesList = roles;
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions;
    },
    SET_ROUTER: (state, resourceTree) => {
      state.resourceTree = resourceTree;
    },
    // SET_ORG: (state, list) => {
    //   state.orgList = list;
    // },
    SET_MESSAGES: (state, list) => {
      state.messages = list;
    },
  },

  actions: {
    // 登录
    Login({ commit }, userInfor) {
      const username = userInfor.username.trim();
      const password = userInfor.password;
      const captchaKey = userInfor.captchaKey;
      const captcha = userInfor.captcha;
      return new Promise((resolve, reject) => {
        login(username, password, captcha, captchaKey)
          .then((res) => {
            setToken(res.result);
            commit("SET_TOKEN", res.result);
            resolve(res.result);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 获取用户信息
    GetInfo({ commit, state, dispatch }) {
      return new Promise(async (resolve, reject) => {
        try {
          let userInfor = getUserInfo();
          let result = {};
          if (userInfor.base && userInfor.base.id) {
            result = userInfor;
          } else {
            let res = await getInfo(state.token);
            result = res.result;
            setUserInfo(result);
          }
          dispatch("GetHead", result.base);
          if (result.auth.roles && result.auth.roles.length > 0) {
            // 验证返回的roles是否是一个非空数组
            commit(
              "SET_ROLES",
              result.auth.roles.map((prams) => prams.value)
            );
            commit("SET_ROLES_LIST", result.auth.roles);
            let data = menuListFilter(result.auth.resourceTree.children, 3);
            // 按钮按钮权限字符
            commit("SET_PERMISSIONS", data);
            commit("SET_ROUTER", result.auth.resourceTree);
          } else {
            commit("SET_ROLES", ["ROLE_DEFAULT"]);
          }
          commit("SET_USER_INFO", result.base);
          // dispatch("GetMation");
          dispatch("GetMessages");
          resolve(result);
        } catch (error) {
          reject(error);
        }
      });
    },
    // 头像
    GetHead({ commit }, user) {
      const avatar = user.headPortraitPath
        ? configs.imgUrl + user.headPortraitPath
        : require("@/assets/images/morenImg.png");
      commit("SET_AVATAR", avatar);
    },
    // 单位
    // GetMation({ commit }) {
    //   orgData().then(({ result }) => {
    //     commit("SET_ORG", result);
    //   });
    // },
    // 消息统计总数
    GetMessages({ commit, state }) {
      getCount(state.userInfo.id).then(({ result }) => {
        commit("SET_MESSAGES", result);
      });
    },
    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token)
          .then(() => {
            commit("SET_TOKEN", "");
            commit("SET_ROLES", []);
            commit("SET_ROUTER", []);
            commit("SET_PERMISSIONS", []);
            removeToken();
            removeUserInfo();
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise((resolve) => {
        commit("SET_TOKEN", "");
        commit("SET_ROLES", []);
        commit("SET_ROUTER", []);
        commit("SET_PERMISSIONS", []);
        removeToken();
        removeUserInfo();
        resolve();
      });
    },
  },
};

export default user;
